package com.timing.feedback.config;


import com.timing.feedback.auth.*;
import com.timing.feedback.filter.JwtAuthenticationTokenFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * @program: boot-security
 * @description:
 * @author: chen wei hao
 * @create: 2021-05-07 15:24
 */
//@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Resource
    private PasswordEncoder passwordEncoder;


    @Resource(name = "userDetailsService")
    private UserDetailsService userDetailsService;

    @Resource
    private WebAuthenticationSuccessHandler webAuthenticationSuccessHandler;

    @Resource
    private WebAuthenticationFailureHandler webAuthenticationFailureHandler;

    @Resource
    private WebLogoutHandler webLogoutHandler;

    @Resource
    private WebLogoutSuccessHandler webLogoutSuccessHandler;

    @Resource
    private WebAuthenticationEntryPoint webAuthenticationEntryPoint;

    @Resource
    private WebAccessDeniedHandler webAccessDeniedHandler;


    @Resource
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(passwordEncoder);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
      super.configure(web);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
            .anyRequest()
            .permitAll()
            //解决跨域问题
            .and()
            .csrf().disable()
            //禁用session
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and().headers().cacheControl();

        //处理异常情况：认证失败和权限不足
        http.exceptionHandling().authenticationEntryPoint(webAuthenticationEntryPoint).accessDeniedHandler(webAccessDeniedHandler);

        //在UsernamePasswordAuthenticationFilter 之前添加 JwtAuthenticationTokenFilter
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        //登录
        http.formLogin().loginProcessingUrl("/login")
            .successHandler(webAuthenticationSuccessHandler)
            .failureHandler(webAuthenticationFailureHandler);

        //退出
        http.logout().addLogoutHandler(webLogoutHandler).logoutSuccessHandler(webLogoutSuccessHandler).deleteCookies("JSESSIONID");

    }
}
